<template>
  <view class="content">
    <!-- <view class="tips-warp" v-show="show_online">
			<text @click="jumpUrl('/pages/map/index')">已连接服务中心</text>
			<image class="close" mode='aspectFit' src='/static/img/index-close.png' @click="show_online = false"></image>
		</view> -->
    <view class="app-title ac"> </view>

    <view class="top-bg">
      <view class="app-title ac">
        <text class="fl">数字化运维系统</text>
        <view class="fr" @click.prevent="toggleLoginOut">
          <view>
            <text class="username">
              {{ userInfoName }}
            </text>
          </view>
          <!-- <image class="user" src="/static/img/leader.png"></image> -->
        </view>
      </view>
      <image class="logo" src="/static/img/new/1.png"></image>
      <image class="title" src="/static/img/new/88.png"></image>
      <!-- <view class="search">
				<image class="search-icon" src="/static/img/search.jpg"></image><image class="clear" src="/static/img/coolicon.png"></image>
				<input type="text" v-model="search"/><b style="vertical-align: top;margin-left:8px">取消</b>
			</view> -->
      <!-- <image class="index-logo" src="/static/img/index-logo.jfif"></image> -->
      <view class="image_box"></view>
    </view>
    <view class="bottom_bg">
      <view class="bottom_bg_img"> </view>
    </view>
    <view class="index-content-box">
      <view class="index-content">
        <view class="manager ac">
          <!-- 			<view class="item fl">
						<image class="icon" src="/static/img/index-icon4.png" @click="showSubmenu('device')"></image>
						<view>设备管理</view>
					</view> -->
          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-router8.png"
              @click="jumpUrl('/pages/assetManag/index')"
            ></image>
            <view>资产管理</view>
          </view> -->
          <!-- <view class="item fl"> -->
          <!-- 			<image class="icon" src="/static/img/index-icon.png" @click="jumpUrl('/pages/alarm/index')">
						</image>
						<view>报警管理</view>
						<view v-show="alarm != 0" class="num ac">{{
            alarm > 99 ? "99+" : alarm
          }}</view> -->
          <!-- <image
              class="icon"
              src="/static/img/index-icon.png"
              @click="showSubmenu('alarm')"
            >
            </image>
            <view>报警管理</view>
            <view v-show="alarm != 0" class="num ac">{{
              alarm > 99 ? "99+" : alarm
            }}</view> -->
          <!-- </view> -->
          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-icon7.png"
              @click="jumpUrl('/pages/monitorCurve/index')"
            ></image>
            <view>监测管理</view>
          </view> -->
          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-icon2.png"
              @click="showSubmenu('faultClosedLoop')"
            >
            </image>
            <view>问题管理</view>
            <view v-show="question != 0" class="num ac">{{
              question > 99 ? "99+" : question
            }}</view>
          </view> -->

          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-icon3.png"
              @click="showSubmenu('order')"
            >
            </image>
            <view>工单管理</view>
            <view v-show="work != 0" class="num ac">{{
              work > 99 ? "99+" : work
            }}</view>
          </view>
 -->
          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-icon4.png"
              @click="showSubmenu('emergency')"
            ></image>
            <view>应急处置</view>
            <view v-show="emergence != 0" class="num ac">{{
              emergence > 99 ? "99+" : emergence
            }}</view>
          </view>
 -->
          <!-- <view class="item fl">
            <image
              class="icon"
              src="/static/img/index-router.png"
              @click="jumpUrl('/pages/docManagement/index')"
            ></image>
            <view>图纸管理</view>
          </view> -->
          <!--	<view class="item fl">
					<image class="icon" src="/static/img/index-icon4.png"
						@click="jumpUrl('/pages/productCenter/index')"></image>
					<view>生产中心</view>
				</view> -->
          <!-- <view class="item fl">
          <image
            class="icon"
            src="/static/img/index-icon4.png"
            @click="showSubmenu('faultClosedLoop')"
          >
          </image>
          <view>问题管理</view>
        </view> -->
          <view class="item fl" v-for="(item, index) in menuList" :key="index">
            <image
              class="icon"
              :src="'/static/img/	' + item.imgSrc"
              @click="clickMenu(item.path, item.type, item)"
            ></image>
            <view>{{ item.title }}</view>
          </view>
          <view class="clearfix"></view>
        </view>

        <view
          class="manager ac flex"
          style="margin-top: 20px; padding-left: 20px; padding-right: 20px"
          v-show="message != ''"
          @click="showMessage"
        >
          <view class="tips_left">
            <image
              src="/static/img/index-bell.png"
              mode="aspectFill"
              class="bell_img"
            ></image>
            <view class="tips_left_content">
              <view class="tips_left_title">系统通知</view>
              <view>{{ message }}</view>
            </view>
          </view>
          <image
            class="arrow_img"
            src="/static/img/arrow.png"
            mode="aspectFit"
          ></image>
          <view class="extra">您有1条通知</view>
        </view>

        <!-- <view class="moudler"> -->
        <!-- <view class="fl" @click="jumpUrl('/pages/emergencyList/list')"> -->
        <!-- 		<view class="fl">
						<view><b>应急处理指南</b></view>
						<view>应急好帮手</view>
					</view>
					<view class="fr"> -->
        <!-- <view class="fr" @click="jumpUrl('/pages/monthly/list')"> -->
        <!-- <view><b>运营周/月报</b></view>
						<view>运营的小字段</view>
					</view>

					<view class="clearfix"></view>
				</view> -->
        <view class="lineSplit"></view>
        <!-- <view
          v-for="(routerItem, routerIndex) in indexRouterList"
          :key="routerIndex"
          :class="[
            submenuShowed == 'faultClosedLoop'
              ? 'fault-closed-loop index-router ac'
              : 'index-router ac',
          ]"
          v-show="submenuShowed == routerItem[0]"
        >
          <view
            v-for="(item, index) in routerItem[1]"
            :key="index"
            class="fl item childMenu"
            @click="jumpUrl(item.routerUrl, item)"
          >
            <image class="icon" :src="'/static/img/	' + item.imageUrl"></image>
            <view>{{ item.title }}</view>
            <view v-if="item.count" class="num ac">{{ item.count }}</view>
          </view>
          <view class="clearfix"></view>
        </view> -->
        <view
          v-show="subMenuList.length"
          :class="[
            currentMenu.id == 1003
              ? 'fault-closed-loop index-router ac'
              : 'index-router ac',
          ]"
        >
          <view
            v-for="(item, index) in subMenuList"
            :key="index"
            class="fl item childMenu"
            @click="jumpUrl(item.path, item)"
          >
            <image class="icon" :src="'/static/img/	' + item.imgSrc"></image>
            <view>{{ item.title }}</view>
            <view v-if="item.count" class="num ac">{{ item.count }}</view>
          </view>
          <view class="clearfix"></view>
        </view>
      </view>
    </view>
    <LoginOut
      v-show="show_out"
      :user="user"
      @closeFull="toggleLoginOut"
      @togglePush="togglePush"
      @showCheckupdata="showCheckupdata"
    >
    </LoginOut>
    <zy-update
      theme="green"
      ref="ZyUpdate"
      @showupdateTips="showupdateTips"
      @changeUpdate="changeUpdate"
      :h5preview="true"
      oldversion="1.1.3"
      :autocheckupdate="false"
      :appstoreflag="false"
      :updateurl="update_url"
    ></zy-update>
  </view>
</template>

<script>
import { mapState } from "vuex";
import { WORK_CONTROLLER } from "../../api/pms";
// var jpushModule = uni.requireNativePlugin("JG-JPush");
import LoginOut from "../../components/index/loginOut";
import ZyUpdate from "../../uni_modules/zy-upgrade-master/zy-upgrade.vue";

// import { DICCONTROLLER, LINECONTROLLER, ORGCONTROLLER } from "../../api/sys";
import { DICCONTROLLER } from "../../api/sys";
// #ifdef APP-PLUS
// #endif
import { initSpeech } from "../../utils/ttsSpeech.js";
import { arrayToTree } from "../../utils/utils.js";

export default {
  components: {
    LoginOut,
    ZyUpdate,
  },
  data() {
    return {
      alarmShowFlag: true,
      message: "",
      user: {},
      work: 0,
      question: 0,
      alarm: 0,
      emergence: 0, // 应急信息数量
      registrationID: "",
      push: "",
      show_out: false,
      show_device: false,
      show_emergency: false,
      show_fault: false,
      show_online: true,
      socketTask: null,
      socketStatus: "",
      indexRouterList: [
        [
          "alarm",
          [
            {
              routerUrl: "/pages/alarm/index?type=0",
              imageUrl: "index-router.png",
              title: "实时报警",
            },
            {
              routerUrl: "/pages/alarm/index?type=1",
              imageUrl: "index-router2.png",
              title: "历史报警",
            },
          ],
        ],
        [
          "emergency",
          [
            {
              routerUrl: "/pages/emergency/emergencyInfo/index",
              imageUrl: "index-router.png",
              title: "应急信息",
            },
            {
              routerUrl: "/pages/emergency/historyList/index",
              imageUrl: "index-router2.png",
              title: "应急记录",
            },
            {
              routerUrl: "/pages/emergency/emergencyInfo/index?param=process",
              imageUrl: "index-router7.png",
              title: "进程看板",
            },
          ],
        ],
        [
          "device",
          [
            {
              routerUrl: "/pages/firstWarehousing/index",
              imageUrl: "index-router.png",
              title: "首次入库",
            },
            {
              routerUrl: "/pages/noWarehousing/index",
              imageUrl: "index-router2.png",
              title: "非首次入库",
            },
            {
              routerUrl: "/pages/deviceOut/index",
              imageUrl: "index-router3.png",
              title: "出库",
            },
            {
              routerUrl: "/pages/device/deviceDown",
              imageUrl: "index-router4.png",
              title: "器材下道",
            },
            {
              routerUrl: "/pages/device/deviceOn",
              imageUrl: "index-router5.png",
              title: "器材上道",
            },
            {
              routerUrl: "/pages/codeQuery/index",
              imageUrl: "index-router6.png",
              title: "器材扫码查询",
            },
            {
              routerUrl: "/pages/useDevice/index",
              imageUrl: "index-router7.png",
              title: "在用设备台账",
            },
            {
              routerUrl: "/pages/material/index",
              imageUrl: "index-router8.png",
              title: "器材台账",
            },
            // {
            //   routerUrl: "/pages/device/changeDevice",
            //   imageUrl: "index-router8.png",
            //   title: "更换设备",
            // },
          ],
        ],
        [
          "faultClosedLoop",
          [
            {
              routerUrl: "/pages/faultClosedLoop/createFaultTicket",
              imageUrl: "index-router.png",
              title: "创建问题单",
            },
            {
              routerUrl: "/pages/faultClosedLoop/toHandle",
              imageUrl: "index-router2.png",
              title: "我的待办",
            },
            {
              routerUrl: "/pages/faultClosedLoop/initiatingProcess",
              imageUrl: "index-router3.png",
              title: "我发起的流程",
            },
            {
              routerUrl: "/pages/faultClosedLoop/handleProcess",
              imageUrl: "index-router4.png",
              title: "我处理的流程",
            },
            {
              routerUrl: "/pages/faultClosedLoop/facilityFailure",
              imageUrl: "index-router6.png",
              title: "运营线路设施设备问题",
            },
            {
              routerUrl: "/pages/faultClosedLoop/nonFacilityFailure",
              imageUrl: "index-router7.png",
              title: "非设施设备问题",
            },
          ],
        ],
        [
          "order",
          [
            {
              routerUrl: "/pages/productCenter/list?typeid=2",
              imageUrl: "index-router2.png",
              title: "我的工单",
              count: 0,
            },
            {
              routerUrl: "/pages/productCenter/list?typeid=1",
              imageUrl: "index-router.png",
              title: "预备工单",
              count: 0,
            },

            {
              routerUrl: "/pages/productCenter/list?typeid=3",
              imageUrl: "index-router3.png",
              title: "历史工单",
              count: 0,
            },

            {
              routerUrl: "/pages/productCenter/createOrder?typeid=2",
              imageUrl: "index-router7.png",
              title: "创建工单",
            },
          ],
        ],
      ],
      submenuShowed: "",
      alarmTaskSocketID: "",
      alarmTaskSocket: null,
      update_url: "",
      version: "1.1.3",
      userInfoName: "",
      menuList: [],
      subMenuList: [],
      currentMenu: {},
    };
  },
  computed: {
    linsenMessage() {
      return this.$store.state.message;
    },
    ...mapState(["wsEmerValue", "emerUpdataValue", "userInfo", "speechValue"]),
  },
  watch: {
    linsenMessage: {
      handler(n, o) {
        this.message = n;
        this.push = this.$store.state.extras;
        for (let i in this.push) {
          let obj = JSON.parse(this.push[i]);
          if (obj.msgType == "alarm") {
            this.alarm += 1;
          }
        }
      },
    },
    wsEmerValue: {
      handler(newVal, oldVal) {
        this.getEmergenceList();
      },
    },
    emerUpdataValue: {
      handler(newVal, oldVal) {
        if (!newVal && newVal != "delete") return;
        this.getEmergenceList();
      },
    },
    speechValue: {
      handler(newVal, oldVal) {
        console.log("newVal---newVal", newVal);
        if (newVal && this.$store.state.speechConfig) {
          this.getTotal();
        }
      },
    },
  },
  onShow() {
    if (!uni.getStorageSync("userId")) {
    } else {
      if (!this.alarmShowFlag) {
        this.getUserInfo();
      } else {
      }
    }
    if (
      this.$store.state.websocketFromOnce === "login" ||
      this.socketStatus === ""
    ) {
      // if (this.socketStatus !== '') {
      // 	this.socketTask.closeSocket()
      // }

      // 初始化应急处置的websocket连接
      if (!this.alarmShowFlag) {
        this.websocketInit();
      } else {
      }
      this.$store.state.websocketFromOnce = "";
    }
    this.getEmergenceList();
    this.getDicData();
    this.userInfoName = this.userInfo.name;
    this.showSubmenu(this.$store.state.menuOrderId);
    console.log("this.userInfoName");
    // #ifdef APP-PLUS
    plus.screen.lockOrientation("portrait-primary");
    // #endif
    if (this.currentMenu.id == 1004) this.getTotal();
  },
  onHide() {
    this.alarmTaskSocket && this.alarmTaskSocket.close();
    this.alarmTaskSocket = null;
  },
  created() {
    // this.checkUpdate("auto");
    this.handleMenus();
  },
  onBackPress(e) {
    uni.showModal({
      title: "提示",
      content: "是否要退出应用程序？",
      success: function (res) {
        if (res.confirm) {
          plus.runtime.quit();
        } else if (res.cancel) {
        }
      },
    });
    return true; //表示禁止默认返回
  },
  methods: {
    handleMenus() {
      this.menuList = [];
      let appMenus = this.$store.state.userInfo.appMenus || [];
      let menuArr = arrayToTree(appMenus);
      this.menuList =
        (menuArr.length && menuArr[0] && menuArr[0].children) || [];
      this.sortData(this.menuList);
      if (this.menuList.length && this.menuList[0].type == 0) {
        this.clickMenu(this.menuList[0].path, 0, this.menuList[0]);
      }
    },
    clickMenu(url, type, item) {
      if (type == 1) {
        this.jumpUrl(url, item);
      } else if (type == 0) {
        this.currentMenu = { ...item };
        this.subMenuList = item.children || [];
        this.sortData(this.subMenuList);
        if (item.id == 1004) {
          this.subMenuList = this.subMenuList.map((item) => {
            return {
              ...item,
              count: 0,
            };
          });
          this.getTotal();
        }
        console.log(this.subMenuList, "subMenuList");
      }
    },
    sortData(list) {
      if (Array.isArray(list) && list.length)
        return list.sort((a, b) => a.seq - b.seq);
    },
    changeUpdate() {},
    showCheckupdata() {
      this.checkUpdate();
    },
    checkUpdate(param) {
      this.update_url =
        this.$store.state.baseUrl + WORK_CONTROLLER.Get_App_Version;
      console.log("this.update_url", this.update_url);
      this.$nextTick(() => {
        this.$refs.ZyUpdate.check_update(param);
      });
    },
    showupdateTips(flag) {
      if (flag == 0) {
        uni.showModal({
          title: "提示",
          content: "已经是最新版本",
        });
      }
    },
    async getDicData() {
      const dicData = await this.fetch(
        "post",
        this.$store.state.baseUrl + DICCONTROLLER.DICLISTSBYDICTYPE,
        {
          dicClass: "appServer", // app服务  appServer
        }
      );
      dicData &&
        dicData.data &&
        Array.isArray(dicData.data) &&
        dicData.data.forEach((item) => {
          this.$store.state.appServer[item.code] = item.value;
        });
    },
    async getTotal() {
      this.getTotalByID(1);
      this.getTotalByID(3);
      // if (this.userInfo?.allWorkOrder) await this.getTotalByID(4);
      this.getTotalByID(4);
      this.getCountMyOrder();
      // this.$forceUpdate()
    },

    async getTotalByID(typeID) {
      let params;
      if (typeID == 1) {
        params = {
          woState: "待派单,进行中,准备中,待接单",
          pageNum: 1,
          pageSize: 20,
          userid: this.getUserId(),
          managementOrg: this.userInfo?.org?.num,
          // major: this.userInfo?.roles && this.userInfo?.roles[0].major
        };
      } else if (typeID == 3) {
        params = {
          woState: "已完成",
          pageNum: 1,
          pageSize: 20,
          userid: this.getUserId(),
          managementOrg: this.userInfo?.org?.num,
          historyQuery: 1,
          // major: this.userInfo?.roles && this.userInfo?.roles[0].major
        };
      } else if (typeID == 4) {
        params = {
          woState: "",
          pageNum: 1,
          pageSize: 20,
          managementOrg: this.userInfo?.org?.num,
          // major: this.userInfo?.roles && this.userInfo?.roles[0].major
        };
      }

      await this.fetch(
        "get",
        this.$store.state.baseUrl + WORK_CONTROLLER.GET_ALL_WORKORDER_LIST,
        params,
        false
      ).then((res) => {
        if (res.success && res.data) {
          let findIndex = this.indexRouterList.findIndex(
            (item) => item[0] == "order"
          );
          console.log("res.data.totalElements", res.data.totalElements);
          if (typeID == 1) {
            this.indexRouterList[findIndex][1][1].count = Number(
              res.data.totalElements
            );
            let index = this.getIndex(1016); // 预备工单
            this.setCount(index, res.data.totalElements);
          }
          if (typeID == 3) {
            this.indexRouterList[findIndex][1][2].count = Number(
              res.data.totalElements
            );
            let index = this.getIndex(1017); // 历史工单
            this.setCount(index, res.data.totalElements);
          }
          if (typeID == 4) {
            this.indexRouterList[findIndex][1][3].count = Number(
              res.data.totalElements
            );
            let index = this.getIndex(1018); // 全部工单
            this.setCount(index, res.data.totalElements);
          }
        }
      });
    },
    async getCountMyOrder() {
      let params = {
        pageNum: 1,
        pageSize: 999999,
        managementOrg: this.userInfo?.org?.num,
        // major: this.userInfo?.roles && this.userInfo?.roles[0].major
      };
      await this.fetch(
        "get",
        this.$store.state.baseUrl + WORK_CONTROLLER.GET_COUNT_MYORDER,
        params,
        false
      ).then((res) => {
        if (res.success) {
          let findIndex = this.indexRouterList.findIndex(
            (item) => item[0] == "order"
          );
          this.indexRouterList[findIndex][1][0].count = Number(res.data);
          let index = this.getIndex(1015); // 我的工单
          this.setCount(index, res.data);
        }
      });
    },
    getIndex(id) {
      let index = this.subMenuList.findIndex((item) => item.id == id);
      return index;
    },
    setCount(index, val) {
      this.subMenuList[index].count = Number(val);
    },
    getEmergenceList() {
      let params = {
        personId: this.getUserId(),
        lineId: "",
        level: "",
        type: 0,
        faultId: "",
      };
      this.fetch(
        "GET",
        this.$store.state.baseEmerUrl + "/emergency/faultList",
        params,
        false
      ).then((res) => {
        if (res.success && res.data) {
          this.emergence = res.data.length;
        } else {
          console.error(res);
        }
      });
    },
    toggleLoginOut() {
      this.show_out = !this.show_out;
    },
    jumpUrl(url, param) {
      uni.navigateTo({
        url,
      });
    },
    showSubmenu(option) {
      // this.submenuShowed = ""; // 切换效果
      this.submenuShowed = option;
      this.$store.commit("setOrderId", this.submenuShowed);
      console.log("this.submenuShowed", this.submenuShowed);
      if (option == "order") {
        console.log("sssss---", option);
        if (this.userInfo?.allWorkOrder) {
          if (this.indexRouterList[4][1][3].title !== "全部工单")
            this.indexRouterList[4][1].splice(3, 0, {
              routerUrl: "/pages/productCenter/list?typeid=4",
              imageUrl: "index-router4.png",
              title: "全部工单",
              count: 0,
            });
        }
        this.getTotal();
      }
    },
    showMessage() {
      if (this.push != "") {
        for (let i in this.push) {
          let obj = JSON.parse(this.push[i]);
          if (obj.msgType == "alarmWeek") {
            this.$store.commit("clearMessage", "");
            uni.navigateTo({
              url: "/pages/monthly/detail?id=" + obj.id,
            });
          } else if (obj.msgType == "alarm") {
            this.$store.commit("clearMessage", "");
            uni.navigateTo({
              url: "/pages/alarm/alarmDetail?id=" + obj.id,
            });
          }
        }
      }
    },
    getJpushId() {
      jpushModule.getRegistrationID((result) => {
        // console.log(result)
        let registerID = result.registerID;
        this.registrationID = registerID;
        jpushModule.addTags({
          sequence: 1,
          tags: [
            "alarmTag1",
            "alarmTag2",
            "alarmTag3",
            "alarmTag10",
            "alarmTagMonth",
            "alarmTagWeek",
          ],
        });
        // setTimeout(()=>{
        // 	// this.fetch('GET',this.baseUrl()+'/ReportController/pushWeekReport?id=2021-11-15--2021-11-21').then((res)=>{
        // 	// 	console.log(res);
        // 	// })
        // 	this.fetch('GET',this.baseUrl()+'/alarm/testPushAlarm/3').then((res)=>{
        // 		console.log(res);
        // 	})
        // })
      });
      // jpushModule.getAllTags(result => {
      // 	console.log(result);
      // });
    },
    getUserInfoFaultCL() {
      this.fetch(
        "GET",
        this.$store.state.baseFaultUrl + "/getLoginUser",
        {},
        false,
        this.$store.state.faultCLHeader
      ).then((res) => {
        if (res.success) {
          this.$store.state.userInfoFaultCL = res.data;
          uni.setStorageSync("username", res.data.name);
          this.judegeFaultClMenu(res.data.menus);
        }
      });
    },
    judegeFaultClMenu(menus) {
      let menusAll = this.indexRouterList[2][1].map((item) => item.title);
      let menusFilter = menus
        .map((item) => item.meta.title)
        .filter((value) => menusAll.indexOf(value) != -1);
      this.indexRouterList[2][1] = this.indexRouterList[2][1].filter(
        (item) => menusFilter.indexOf(item.title) != -1
      );
    },
    getUserInfoAgo() {
      this.fetch("GET", this.baseUrl() + "/user/" + this.getUserId(), {}).then(
        (res) => {
          if (res) {
            this.user = res;
            this.user.avatar != null
              ? (this.user.avatar = this.user.avatar.replace(/\\/g, "/"))
              : "";
            uni.setStorageSync("username", res.username);
            uni.setStorageSync("departid", res.departid);
          }
        }
      );
    },
    getUserPush() {
      this.fetch(
        "GET",
        this.baseUrl() +
          "/user/connect/" +
          this.registrationID +
          "/" +
          this.getUserId()
      ).then((res) => {
        console.log(res);
      });
    },
    togglePush(bol) {
      if (bol) {
        this.getUserPush();
      } else {
        this.fetch(
          "GET",
          this.baseUrl() + "/user/disConnect/" + this.getUserId()
        ).then((res) => {
          console.log(res);
        });
      }
    },
    getUserInfo() {
      let userID = this.getUserId();
      this.fetch(
        "GET",
        this.$store.state.baseEmerUrl + `/api/sys/person/findOneById/${userID}`,
        {},
        false
      ).then((res) => {
        if (res.success && res.data) {
          this.$store.state.userInfo = res.data;
          this.user = res.data;
        } else {
          console.error(res);
        }
      });
    },
    newWebsocketInitAlarm() {
      clearTimeout(this.alarmTaskSocketID);
      this.alarmTaskSocket = uni.connectSocket({
        url: this.$store.state.wsEmerUrl + "/socket/1/alarmQueue",
        complete: () => {},
      });
      this.alarmTaskSocket.onOpen(() => {
        console.log("WebSocket --Alarm----23-- 已连接!");
        clearTimeout(this.alarmTaskSocketID);
      });
      this.alarmTaskSocket.onClose((error) => {
        console.log("WebSocket --Alarm----23--关闭了", error);
        if (error.code != 1000) {
          this.alarmTaskSocketID = setTimeout(() => {
            this.alarmTaskSocket = null;
            this.newWebsocketInitAlarm();
          }, 10000);
        }
      });
      this.alarmTaskSocket.onMessage(() => {
        let alarmToastInfo = JSON.parse(message.data);
        if (alarmToastInfo) {
          this.$store.state.wsAlarmValue = !this.$store.state.wsAlarmValue;
          uni.showToast({
            title: "你有新的报警信息！",
            icon: "none",
            duration: 2000,
          });
        }
      });
    },
    sendPosition() {
      uni.getLocation({
        type: "gcj02",
        geocode: true, //获取城市具体地址
        success: (res) => {
          // console.log('当前位置的经度：' + res.longitude);
          // console.log('当前位置的纬度：' + res.latitude);

          let params = {
            personId: this.getUserId(),
            latitude: res.latitude,
            longitude: res.longitude,
          };

          this.fetch(
            "POST",
            this.$store.state.baseEmerUrl + "/emergency/emergencyLocation",
            params,
            false
          ).then((res) => {
            // console.log("输出定位返回数据：",res)

            // if (res.success) {
            // console.log("定位数据发送成功！")
            // } else {
            // 	this.sendPosition()
            // }

            if (uni.getStorageSync("port") != 8088) {
              setTimeout(() => {
                this.sendPosition();
              }, 5000);
            }
          });
        },
        fail: function (res) {
          console.error(res);
        },
      });
    },
  },
  beforeDestroy() {
    clearTimeout(this.alarmTaskSocketID);
    this.alarmTaskSocket && this.alarmTaskSocket.close();
    this.alarmTaskSocket = null;
    console.log("页面销毁了-----");
  },
};
</script>

<style scoped lang="less">
.username {
  color: #3e3a39;
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 5px;
  font-family: "PingFang SC", "Microsoft YaHei", "Arial", "黑体", "宋体",
    sans-serif;
}

.lineSplit {
  height: 20px;
}

.childMenu {
  position: relative;

  .num {
    top: -10px;
    right: 10%;
  }
}

.content {
  overflow: hidden;
  height: 100vh;
  position: relative;
  background-color: #f2f2f3;
  background: linear-gradient(180deg, #f5f6f8 0%, rgba(245, 246, 248, 0) 100%);
}

.bottom_bg {
  position: absolute;
  background: url("/static/img/new/Nipic_6699049_20190920151412375000.png");
  width: 100%;
  height: 40vh;
  bottom: 0px;
  filter: opacity(2);

  .bottom_bg_img {
    width: 100%;
    height: 100%;
    background: linear-gradient(
      0deg,
      rgba(255, 255, 255, 0.1) 0%,
      rgba(255, 255, 255, 0.6) 100%
    );
  }
}

.tips-warp {
  display: flex;
  position: fixed;
  background: rgba(48, 123, 224, 0.6);
  box-shadow: 2px 2px 4px rgba(0, 0, 0, 0.05);
  bottom: 0;
  left: 0;
  right: 0;
  color: #fff;
  z-index: 999;
  padding: 20px 20px;
  justify-content: space-between;
  align-items: center;
}

.tips-warp .close {
  width: 14px;
  height: 14px;
}

.app-title {
  background-color: #0b6fe9;
}

.top-bg {
  /* padding: 0 16px 0 16px; */
  background: none;
  background-color: #0b6fe9;
  height: 280px;
  position: relative;

  /deep/.app-title {
    padding: 0;
    font-size: 22px;
    position: absolute;
    z-index: 1;
    background: none;
    width: 100%;
    margin: 0 auto;

    .fl {
      padding-left: 30px;
      padding-top: 20px;
    }

    .fr {
      padding-right: 10px;
    }
  }

  .title {
    height: 28px;
    position: absolute;
    z-index: 1;
    width: 155px;
    top: 60px;
    left: 30px;
    font-family: "Microsoft YaHei";
  }

  .logo {
    z-index: 1;
    height: 20vh;
    position: absolute;
    top: 10px;
    left: -30px;
  }

  .user {
    // color: #0B6FE9;
    height: 24px;
    width: 24px;
  }
}

.image_box {
  overflow: hidden;
  position: absolute;
  background: url("/static/img/new/image.png") no-repeat;
  background-size: cover;
  height: 100%;
  width: 100%;
  top: 0;
  // background-position: 60px -70px;
  margin-top: -70px;
  margin-left: 60px;
  box-sizing: border-box;
}

.top-bg > .search {
  position: relative;
  color: #fff;
  line-height: 32px;
}

.top-bg > .search > input {
  width: 84%;
  height: 32px;
  background: #fff;
  border-radius: 20px;
  color: #000000;
  padding: 0 28px;
}

.top-bg > .index-logo {
  height: 120px;
  width: 100%;
}

.tips_left {
  display: flex;
}

.tips_left .tips_left_title {
  color: #000;
  font-size: 18px;
  margin-bottom: 2px;
  font-weight: bold;
}

.tips_left > .tips_left_content {
  display: flex;
  flex-direction: column;
  justify-content: center;
  margin-left: 10px;
  text-align: left;
}

.flex {
  display: flex;
  position: relative;
  align-items: center;
  justify-content: space-between;
}

.extra {
  position: absolute;
  right: 20px;
  padding: 3px 10px;
  top: 0;
  border-radius: 0 0 4px 4px;
  background-color: rgba(222, 84, 44, 0.1);
  color: #de542c;
  font-size: 12px;
}

.bell_img {
  width: 44px;
  height: 44px;
}

.arrow_img {
  width: 14px;
  height: 14px;
}

.top-bg > .search > .search-icon {
  position: absolute;
  top: 9px;
  left: 10px;
  width: 15px;
  height: 15px;
  z-index: 5;
}

.top-bg > .search > .clear {
  position: absolute;
  top: 9px;
  right: 64px;
  width: 15px;
  height: 15px;
  z-index: 5;
}

.index-content-box {
  position: absolute;
  width: 100vw;
  top: 22vh;
  z-index: 2;
}

.index-content {
  width: 90vw;
  margin: 0 auto;
  /* margin-top: -44px; */
}

.index-content > .manager {
  background: #ffffff;
  box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.05), 2px 2px 4px rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  color: rgba(62, 58, 57, 0.7);
  font-size: 12px;
  padding: 20px 0;
  min-height: 205px;
}

.index-content > .manager > .item {
  width: 25%;
  position: relative;
  margin-bottom: 14px;
}

.index-content > .manager > .item:nth-child(5) {
  margin-bottom: 0;
}

.index-content > .index-router > .item {
  width: 25%;
}

.content > .index-content > .fault-closed-loop > .item {
  width: 33%;
}

.index-content > .manager > .item > .icon {
  width: 44px;
  height: 44px;
  margin-bottom: 6px;
}

.index-content > .moudler {
  margin: 16px 0;
}

.index-content > .moudler > .fl,
.index-content > .moudler > .fr {
  width: 48.5%;
  height: 80px;
  padding: 16px 16px 0 16px;
  background-size: 100% 100% !important;
}

.index-content > .moudler > .fl:nth-child(1) {
  background: url("/static/img/index-moudler.png") no-repeat;
}

.index-content > .moudler > .fr:nth-child(2) {
  background: url("/static/img/index-moudler2.png") no-repeat;
}

.index-content > .moudler > .fl:nth-child(1) > view:nth-child(1) {
  color: #39437d;
  font-size: 14px;
  margin-bottom: 6px;
}

.index-content > .moudler > .fl:nth-child(1) > view:nth-child(2) {
  color: rgba(57, 67, 125, 0.7);
  font-size: 12px;
}

.index-content > .moudler > .fr:nth-child(2) > view:nth-child(1) {
  color: #7a3c16;
  font-size: 14px;
  margin-bottom: 6px;
}

.index-content > .moudler > .fr:nth-child(2) > view:nth-child(2) {
  color: rgba(122, 60, 22, 0.7);
  font-size: 12px;
}

.index-content > .index-router {
  background: #ffffff;
  box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.05), 2px 2px 4px rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  padding-top: 20px;
  color: rgba(62, 58, 57, 0.7);
  font-size: 10px;
}

.index-content > .index-router > .item {
  margin-bottom: 20px;
}

.index-content > .index-router > .item > .icon {
  width: 44px;
  height: 44px;
  margin-bottom: 5px;
}

.num {
  position: absolute;
  top: 3px;
  right: 3%;
  background: #fa5151;
  /* width: 15px; */
  // height: 15px;
  font-size: 10px;
  border-radius: 50%;
  color: #fff;
  line-height: 14px;
  padding: 0px 5px;
}
</style>
